package my.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioTest1 {

	public static void main(String[] args) {
		File file = new File("G:\\apache-tomcat-7.0.77new\\logs\\localhost_access_log.2017-06-16.txt");
		System.out.println(file.getAbsolutePath());
		FileInputStream in = null;
		FileChannel fileChannel = null;
		try {
			in = new FileInputStream(file);
			fileChannel = in.getChannel();
			ByteBuffer bbuffer = ByteBuffer.allocate(30 * 1024);
			int length = 0;
			while ((length = fileChannel.read(bbuffer)) > 0) {
				//bbuffer.flip();
				System.out.println(new String(bbuffer.array(), 0, length, "GBK"));
				bbuffer.clear();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fileChannel.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
